package pc.sql;
import java.sql.*;
import java.util.*;

public class ConnectionPool
{
  private Hashtable connections;
  private String dbName = null, user = null, password = null;
  //private final String driverClassName = "com.mysql.jdbc.Driver";
  private String hostName = "localhost:3306";
  private String dbURL = "jdbc:mysql://";
  static {
    try {
     Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      System.out.println(e.getMessage());
    }
  }

  public ConnectionPool(String hostName,String dbName, String user, String password)
  {
      this.hostName = hostName;
      this.dbName = dbName;
      this.user  = user;
      this.password = password;

      connections = new Hashtable();

      connections.put(get_Connection(),Boolean.FALSE);
  }

  public String getDB_Name()
  {
    return dbName;
  }

  private synchronized Connection get_Connection()
  {
    String url = dbURL + hostName + "/" + dbName;
    System.out.println("String DB URL -----------"+url);
    Connection con = null;
    try {
      con = DriverManager.getConnection(url, user, password);
    } catch (SQLException e) {
      System.out.print(e.getMessage());
    }
    return con;
  }

  public synchronized Connection getConnection()
  {
      Connection con = null;

      Enumeration cons = connections.keys();

      synchronized (connections) {
          while(cons.hasMoreElements()) {
              con = (Connection)cons.nextElement();

              Boolean b = (Boolean)connections.get(con);
              if (b == Boolean.FALSE) {
                  try {
                      con.setAutoCommit(true);
                  } catch(SQLException e) {
                    connections.remove(con);
                    con = get_Connection();
                  }
                  //System.out.println("Get Connection " + con.toString());
                  connections.put(con, Boolean.TRUE);
                  return con;
              }
          }
      }

      connections.put(get_Connection(),Boolean.FALSE);

      return getConnection();
  }

  public synchronized void returnConnection(Connection con)
  {
    if (con == null) {
      //System.out.println("Return Connection NULL");
      return;
    }
    if (connections.containsKey(con)) {
      //System.out.println("Return Connection " + con.toString());
      connections.put(con, Boolean.FALSE);
    }
  }

  public void releaseConnection(Connection con)
  {
    if (con != null) {
      if (connections.containsKey(con)) {
        connections.put(con, Boolean.FALSE);
      }
      connections.remove(con);
      //System.out.println("Close Connection " + con.toString());
      try {
        con.close();
      } catch (SQLException e) {
         System.out.println(e.getMessage());
       }

    }
  }

 public void releaseConnections()
 {
   Connection con = null;
   Enumeration cons = connections.keys();
   synchronized (connections) {
     while(cons.hasMoreElements()) {
       con = (Connection)cons.nextElement();
       try {
         con.close();
       } catch (SQLException e) {
         System.out.println(e.getMessage());
       }
     }
   }
   connections.clear();
  }
}
